// pages/demo/demo2.js
var city_list = require('./city.js');
import CommonService from '../../service/CommonService';
Page({
  data: {
    cityList: city_list.city,
    chooseCity: '您还未选择机场！',
    isShowLayer: false,
    chooseIndex: 0,
    len: [],
    code: null,
    codeHeight: null,
    cityHeight: null,
    scrollTop: 0
  },
  onLoad(options) {
    const _ = this;
    const windowHeight = wx.getSystemInfoSync().windowHeight;
    const arr = [];
    this.commonService = new CommonService();
    this.commonService.queryCityGroup().then(result=>{
      _.setData({ cityList: result.data});
      result.data.forEach(current => arr.push(current.cityList.length + 1));
      _.setData({
        codeHeight: (windowHeight - 50) / this.data.cityList.length,
        cityHeight: windowHeight - 50,
        len: arr
      });
    })
    

    
  },
  getCurrentCode(e) {
    var index = 0, sum = 0, self = this;

    for (var i = 0; i < this.data.cityList.length; i++) {
      if (this.data.cityList[i].code === e.target.dataset.code) {
        index = i
        break;
      }
    }
    for (var j = 0; j < index; j++) {
      sum += this.data.len[j];
    }

    this.setData({
      code: e.target.dataset.code,
      scrollTop: sum * 40,
      chooseIndex: index,
      isShowLayer: true
    });

    setTimeout(() => {
      self.setData({ isShowLayer: false })
    }, 500);
  },
  getChooseCity(e) {
    this.setData({ chooseCity: e.target.dataset.city });
  }
})